<?php
/**
 * Created by Silent Night.
 * Author: Silent Night
 * email: silentwolf_wp@163.com
 * Date: 2017/9/28
 * Time: 9:19
 */

namespace app\admin\controller;

use think\Controller;
use think\Db;
use think\Session;

class Hosporder extends Controller
{
    private $_model;
    private $title_field = [
    ];
    public $pagesize = 10;

    public function _initialize()
    {
        $this->_model = new \app\admin\model\HospOrder();
        $this->assign('LoginUserInfo', $this->LoginUserInfo);
        //获取当前用户的权限信息
        $this->assign('ruleLists', $this->LoginUserInfo['CurrPower']);
    }

    /**
     * 列表页
     * @return mixed
     */
    public function index()
    {
        return $this->fetch('index');
    }

    public function lists()
    {
        $ser_key = trim(input("request.ser_key"));//auth
        $ser_val = trim(input("request.ser_val"));//auth
        $ser_stats = trim(input("request.ser_stats"));//auth

        if ($ser_key && $ser_val) {
            $where[$ser_key] = array('like', '%' . $ser_val . '%');
        }

        if ($ser_stats == '0' || $ser_stats == '1' || $ser_stats == '2' || $ser_stats == '3' || $ser_stats == '4') {
            $where['status'] = $ser_stats;
        }
        $where['is_delete'] = '0';

        $lists = $this->_model->lists($where, $this->pagesize, "*", "id desc");
        $lists['data'] = changeUidToUserinfoForLists($lists['data'], 'uid');

        foreach ($lists['data'] as $key => $val) {
            $lists['data'][$key] = $this->changeOrderTime($val);
            $lists['data'][$key]['DoctorInfo'] = changeToDoctor(getDoctorInfo($val['doctor_id']), false);
        }


        $this->assign('lists', $lists);
        $this->assign('request', input("request."));
        return view('lists');
    }

    private function changeOrderTime($orderInfo)
    {
        if ($orderInfo['ctime']) {
            $orderInfo['ctime'] = date("Y-m-d H:i:s", $orderInfo['ctime']);
        }
        if ($orderInfo['ptime']) {
            $orderInfo['ptime'] = date("Y-m-d H:i:s", $orderInfo['ptime']);
        }
        if ($orderInfo['applytime']) {
            $orderInfo['applytime'] = date("Y-m-d H:i:s", $orderInfo['applytime']);
        }
        if ($orderInfo['receipttime']) {
            $orderInfo['receipttime'] = date("Y-m-d H:i:s", $orderInfo['receipttime']);
        }
        if ($orderInfo['closetime']) {
            $orderInfo['closetime'] = date("Y-m-d H:i:s", $orderInfo['closetime']);
        }
        if ($orderInfo['pjtime']) {
            $orderInfo['pjtime'] = date("Y-m-d H:i:s", $orderInfo['pjtime']);
        }

        if ($orderInfo['js_time']) {
            $orderInfo['js_time'] = date("Y-m-d H:i:s", $orderInfo['js_time']);
        }
        if ($orderInfo['canceltime']) {
            $orderInfo['canceltime'] = date("Y-m-d H:i:s", $orderInfo['canceltime']);
        }
        return $orderInfo;
    }

    /**
     * 删除
     * 支持批量删除和单条删除
     * @return mixed
     */
    public function delData()
    {
        $return = [];
        $id = trim(input("request.id"));//auth
        if (empty($id)) {
            $return = ['err' => 1, 'msg' => '操作失败'];
            return json($return);
        }
        $res = $this->_model->updateData(array('is_delete' => 1), $id);
        if ($res) {
            $return = ['err' => 0, 'msg' => '操作成功'];
            return json($return);
        } else {
            $return = ['err' => 3, 'msg' => '操作失败'];
            return json($return);
        }

    }

    /**
     * 退款
     * @return \think\response\Json|\think\response\View
     */
    public function tk()
    {

        $id = trim(input("request.id"));//auth
        $sys_opt = trim(input("request.sys_opt"));//auth
        //查询用户
        $info = $this->_model->getDataForWhere(array('id' => $id));
        if ($sys_opt) {
            $data1 = $_REQUEST['data'];
            $tk_money = $data1['tk_money'];
            if ($tk_money > $info['money']) {
                $return = ['err' => 1, 'msg' => '退款金额大于订单金额'];
                return json($return);
            }
            addOrderTrackLog(array(
                'ctime' => date("Y-m-d H:i:s"),
                'order_sn' => $info['order_sn'],
                'op_uid' => $this->LoginUserInfo['UserInfo']['id'],
                'op_username' => $this->LoginUserInfo['UserInfo']['uname'] . "[" . $this->LoginUserInfo['UserInfo']['nickname'] . "]",
                'act' => "后台发起退款[" . $tk_money . "]元",
            ));
            import("myCal.AsyncHook#class", VENDOR_PATH);
            \AsyncHook::hook(array($this, 'hospOrderPayRefund'), array($info['order_sn'], $tk_money));
            $return = ['err' => 0, 'msg' => '操作成功'];
            return json($return);
        }

        $this->assign('info', $info);
        //设置全局信息
        $title = "退款";
        $this->assign('title', $title);
        $this->assign('id', $id);
        return view('tk');
    }


    /**
     * 查询退款接口
     * @return \think\response\Json|\think\response\View
     */
    public function selecttk()
    {

        $id = trim(input("request.id"));//auth
        $sys_opt = trim(input("request.sys_opt"));//auth
        //查询用户
        $info = $this->_model->getDataForWhere(array('id' => $id));

        $res = hospOrderSelectRefund($info['order_sn']);
        $tishi = "";
        if ($res['err'] == 0) {
            $tishi = "退款次数：" . $res['data']['refund_count'] . "，退款金额：" . ($res['data']['refund_fee'] / 100) . "元";
        } else {
            $tishi = $res['msg'];
        }
        $return = ['err' => 0, 'msg' => '操作成功', 'data' => $tishi];
        return json($return);
    }

    public function hospOrderPayRefund($order_sn, $money = 0)
    {
        $res = hospOrderPayRefund($order_sn, $money);
        if ($res['err'] == 0) {
            $model = new \app\admin\model\HospOrder();
            $model->updateDataForWhere(array(
                'stat' => 3,
            ), array('order_sn' => $order_sn));
            addOrderTrackLog(array(
                'ctime' => date("Y-m-d H:i:s"),
                'order_sn' => $order_sn,
                'op_uid' => 0,
                'op_username' => "",
                'act' => "退款成功",
            ));
        } else {
            addOrderTrackLog(array(
                'ctime' => date("Y-m-d H:i:s"),
                'order_sn' => $order_sn,
                'op_uid' => 0,
                'op_username' => "",
                'act' => "退款失败[" . $res['msg'] . "]",
            ));
        }
    }

    public function close()
    {
        $return = [];
        $id = trim(input("request.id"));//auth
        if (empty($id)) {
            $return = ['err' => 1, 'msg' => '操作失败'];
            return json($return);
        }
        $info = $this->_model->getDataForWhere(array('id' => $id));
        $res = $this->_model->updateData(array('status' => 4), $id);
        if ($res) {
            addOrderTrackLog(array(
                'ctime' => date("Y-m-d H:i:s"),
                'order_sn' => $info['order_sn'],
                'op_uid' => $this->LoginUserInfo['UserInfo']['id'],
                'op_username' => $this->LoginUserInfo['UserInfo']['uname'] . "[" . $this->LoginUserInfo['UserInfo']['nickname'] . "]",
                'act' => "管理员后台取消订单",
            ));
            $return = ['err' => 0, 'msg' => '操作成功'];
            return json($return);
        } else {
            $return = ['err' => 3, 'msg' => '操作失败'];
            return json($return);
        }

    }


    public function addGoodAttr($id, $attr)
    {
        $model = new \app\admin\model\GoodAttribute();
        $data = array(
            'good_id' => $id,
            'attr' => $attr,
        );
        $model->add($data);
    }

    public function infoData()
    {
        $id = trim(input("request.id"));//auth
        $info = $this->_model->getDataForWhere(array('id' => $id));
        $info['UserInfo'] = array();
        if ($info['uid']) {
            $info['UserInfo'] = getUserInfoForID($info['uid']);
        }
        $info['DoctorInfo'] = array();
        if ($info['doctor_id']) {
            $info['DoctorInfo'] = changeToDoctor(getDoctorInfo($info['doctor_id']), false);
        }
        $info = $this->changeOrderTime($info);


        $this->assign('info', $info);
        $title = "详情";

        //获取订单日志
        $orderLogModel = new \app\admin\model\OrderTrackLog();
        $orderLogLists = $orderLogModel->getDataForWheres(array('order_sn' => $info['order_sn']), "*", "id desc");
        $this->assign('orderLogLists', $orderLogLists);

        $this->assign('title', $title);
        $this->assign('id', $id);
        return view('infoData');

    }


    public function editData()
    {

        $id = trim(input("request.id"));//auth
        $sys_opt = trim(input("request.sys_opt"));//auth
        //查询用户
        $info = $this->_model->getDataForWhere(array('id' => $id));

        if ($sys_opt) {
            $data1 = $_REQUEST['data'];
            $content = trim(input("request.conts"));//auth
            $attr = trim(input("request.attr"));//auth
            $w = array(
                'id' => $id
            );
            $data2 = array(
                'content' => $content,
            );
            $data = array_merge($data1, $data2);
            $res = $this->_model->updateDataForWhere($data, $w);

            if ($res || ($res === 0)) {
                $this->editGoodAttr($id, $attr);
                $return = ['err' => 0, 'msg' => '操作成功'];
                return json($return);
            } else {
                $return = ['err' => 3, 'msg' => '操作失败'];
                return json($return);
            }
        }
        $tmpGoodAttribute = getGoodAttr($info['id']);
        $goodAttribute = array();
        if ($tmpGoodAttribute) {
            $goodAttribute = json_decode($tmpGoodAttribute['attr'], true);
        }
        $this->assign('goodAttribute', $goodAttribute);

        $this->assign('info', $info);
        //设置全局信息
        $title = "编辑";
        $this->assign('title', $title);
        $this->assign('id', $id);
        return view('editData');
    }

}